/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.task;

import com.tlf.dyjc5.service.ConstantService;
import com.tlf.dyjc5.service.RsjService;
import com.tlf.dyjc5.service.YsjService;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;
import org.dusin.commonutil.DateUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 已弃用
 * @author tengyu
 */
public class TaskThread extends Thread {

    boolean stop = false;
    private ServletContext servletContext;
    static Logger logger = Logger.getLogger(TaskThread.class);
    boolean isRsjLock = false;
    boolean isYsjLock=false;
    String rsjLockTime = "1:00";  //自动锁定时间
    String ysjLockTime = "28,1:00";      //自动锁定月数据时间
    int rsjLockHour;
    int rsjLockMin;
    int ysjLockDay;
    int ysjLockHour;
    int ysjLockMin;
    
    boolean data5mAutoGen = false; //是否自动生成5分钟数据；
    //booelean 

    public void makeItStop() {
        stop = true;
    }

    @Override
    public void run() {
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ConstantService constantService = (ConstantService) context.getBean("constantService");
        
        while (!stop) {// 线程未中断执行循环  
            try {
                Thread.sleep(60 * 1000); //每隔1分钟执行一次

                Calendar now = Calendar.getInstance();
                Calendar yestoday = Calendar.getInstance();
                yestoday.add(Calendar.DAY_OF_MONTH, -1);
                //logger.debug(yestoday);

                Calendar lastMonth = Calendar.getInstance(); //上个月
                lastMonth.add(Calendar.MONTH, -1);

                logger.info(this.getName() + "," + DateUtil.get(new Date(now.getTimeInMillis())) + ":working");

                int year = now.get(Calendar.YEAR);
                int month = now.get(Calendar.MONTH) + 1;
                int day = now.get(Calendar.DAY_OF_MONTH);
                int hour = now.get(Calendar.HOUR_OF_DAY);
                int min = now.get(Calendar.MINUTE);

                int yesYear = yestoday.get(Calendar.YEAR);              //昨天的年月日
                int yesMonth = yestoday.get(Calendar.MONTH) + 1;
                int yesDay = yestoday.get(Calendar.DAY_OF_MONTH);
                logger.debug(yesMonth);
                

                int lastMonthYear = lastMonth.get(Calendar.YEAR);              //上个月的年月
                int lastMonthMonth = lastMonth.get(Calendar.MONTH) + 1;

                
                String rsjSwith;
                String ysjSwith;



                if (hour == 0 && (min > 0 && min <= 2)) { //0点0，1，2分做初始设置，日数据初始设置
                    isRsjLock = false;
                }

                if (day == 1 && hour == 0 && (min > 0 && min <= 2)) { //0点0，1，2分做初始设置，日数据初始设置                   
                    isYsjLock = false;
                }

                try {
                    rsjSwith = constantService.getValue("日数据定时锁定", "01");
                } catch (org.springframework.dao.EmptyResultDataAccessException e) {
                    logger.warn("没有设置日数据锁定开关");
                    rsjSwith = "关";
                }
                try {
                    ysjSwith = constantService.getValue("月数据定时锁定", "01");
                } catch (org.springframework.dao.EmptyResultDataAccessException e) {
                    ysjSwith = "关";
                    logger.warn("没有设置月数据锁定开关");
                }
                try {
                    rsjLockTime = constantService.getValue("日数据定时锁定时间", "01");
                    String[] timeItems = rsjLockTime.split(":");
                    rsjLockHour = Integer.parseInt(timeItems[0]);
                    rsjLockMin = Integer.parseInt(timeItems[1]);
                } catch (org.springframework.dao.EmptyResultDataAccessException e) {
                    logger.warn("没有设置日数据锁定时间参数");
                    rsjSwith = "关";
                } catch (Exception e) {
                    rsjSwith = "关";
                    logger.warn("日数据锁定时间格式错误");
                }
                try {
                    ysjLockTime = constantService.getValue("月数据定时锁定时间", "01");
                    String[] timeItems = ysjLockTime.split("[,:]");
                    ysjLockDay = Integer.parseInt(timeItems[0]);
                    ysjLockHour = Integer.parseInt(timeItems[1]);
                    ysjLockMin = Integer.parseInt(timeItems[2]);

                } catch (org.springframework.dao.EmptyResultDataAccessException e) {
                    logger.warn("没有设置月数据锁定时间参数");
                    ysjSwith = "关";
                } catch (Exception e) {
                    logger.warn("设置的月数据锁定时间格式错误");
                    ysjSwith = "关";
                }
                
                try {
                    String strValue = constantService.getValue("5分钟数据生成", "01");
                    strValue=strValue.trim();
                    if("开".equals(strValue)||"on".equalsIgnoreCase(strValue)||"true".equalsIgnoreCase(strValue)){
                        data5mAutoGen = true;
                    }else{
                        data5mAutoGen = false;
                    }

                } catch (org.springframework.dao.EmptyResultDataAccessException e) {
                    logger.warn("没有设置月数据锁定时间参数");
                    data5mAutoGen = false;
                } catch (Exception e) {
                    logger.warn("设置的月数据锁定时间格式错误");
                    data5mAutoGen = false;
                }
                
                
                
                logger.debug(isRsjLock+rsjSwith);
                if (!isRsjLock && rsjSwith.equals("开")) {  //锁定昨日数据
                    if (hour == rsjLockHour && (min >= rsjLockMin && min <= rsjLockMin + 3)) {
                        try {
                            String orgCode = constantService.getValueDown("orgCode", "01");

                            RsjService rsjService = (RsjService) context.getBean("rsjServiceProxy");
                            rsjService.trLockByOrg(orgCode, String.valueOf(yesYear * 10000 + yesMonth * 100 + yesDay), -1, -1);
                            logger.info(String.valueOf(yesYear * 10000 + yesMonth * 100 + yesDay) + "日数据已锁定");
                            isRsjLock = true;
                        } catch (Exception e) {     //预防抛出AlreadyLock异常，而跳过月数据的自动锁定
                            logger.warn(e.getMessage());
                        }
                    }
                }

                if (!isYsjLock && ysjSwith.equals("开")) {  //锁定月数据
                    String date;
                    if (ysjLockDay >= 20) {     //大于20，锁定本月数据
                        date = String.valueOf(year * 100 + month);
                    } else {             //日期为月头，锁定上月数据
                        date = String.valueOf(lastMonthYear * 100 + lastMonthMonth);
                    }

                    if (day == ysjLockDay && hour == ysjLockHour && (min >= ysjLockMin && min <= ysjLockMin + 3)) {
                        String orgCode = constantService.getValueDown("orgCode", "01");
                        YsjService ysjService = (YsjService) context.getBean("ysjServiceProxy");
                        //ysjService.trLockByOrg(orgCode, date, -1, -1);
                        logger.info(date + "月数据已锁定");
                        isYsjLock = true;
                    }
                }

            } catch (InterruptedException e) {
                //System.out.print(this.getName());
                e.printStackTrace();
            } catch (Throwable t) {
                //System.out.print(this.getName());
                t.printStackTrace();
            }
        }
        logger.info("结束执行线程***********");
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }
}
